<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Database_Result_Mysql {
	private $_result = null;
	
	public function __construct($result) {
		$this->_result = $result;
	}
	
	public function __destruct() {
		$this->freeResult();
	}

	/**
	 * Get returned rows count (SELECT)
	 */
	public function getNumRows() {
		$result = 0;
		
		if ($this->_result) {
			$result = mysql_num_rows($this->_result);
		}
		
		return $result;
	}
	
	/**
	 * Free result
	 */
	public function freeResult() {
		if (is_resource($this->_result)) {
			mysql_free_result($this->_result);
		}
		$this->_result = null;
	}

	/**
	 * Return single row from select query
	 */
	public function row($row_num = 0) {
		$result = null;
		
		if ($this->_result) {
			if ($row_num < $this->getNumRows()) {
				if (mysql_data_seek($this->_result, $row_num)) {
					$result = mysql_fetch_assoc($this->_result);
				}
			}
		}
		$this->freeResult();

		return $result;
	}
	
	/**
	 * Return number of rows from select query
	 */
	public function rows($rows_num = 0) {
		$result = array();

		if ($this->_result) {
			$count = 0;
			while ($row = mysql_fetch_assoc($this->_result)) {
				$result[] = $row;
				$count++;
				if (($rows_num > 0) && ($count >= $rows_num)) {
					break;
				}
			}
		}
		$this->freeResult();

		return $result;
	}
}
